<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\Install;
use app\admin\model\Modular;
use app\admin\model\MyAuth;
use app\admin\model\MyChildren;
use builder\Builder;
use think\Db;
use think\Request;

class Index
{
    public function envs(){
        echo '<pre>';
        print_r(env());
    }
    public function index(){
        if(!$user=session('adminuser')){
            return redirect('index/signin');
        }
        $tup = getSysTem('logo','img');
        if($tup){
            $yiqian = config('modular.');
            foreach ($yiqian as $key=>$value){
                if($value['dir']==session('modular')){
                    $value['img'] = $tup;
                }
                $yiqian[$key] = $value;
            }
            Modular::changeConfig($yiqian);
        }
        $lianbiao = Db::name(getTable('models'))
            ->where('type','联表')
            ->where('status',1)
            ->field('id,name')
            ->select();
        $column = MyChildren::getDatas(getTable('column'),true);
//        echo 'haha';exit;
        $mokuai = Db::name(getTable('mokuais'))
            ->where('status',1)
            ->select();
        foreach ($mokuai as $key=>$value){
            $value['column'] = MyChildren::getDatas(getTable('column'),true,['mokuai_id'=>$value['id']]);
            $mokuai[$key] = $value;
        }
        $welcome['fangwenliang'] = getSysTem('fangwen');
        if(!$welcome['fangwenliang']){
            $welcome['fangwenliang'] = 0;
        }
        $welcome['fujian'] = Db::name(getTable('enclosure'))
            ->count();
        $welcome['yonghu'] = Db::name(getTable('users'))
            ->count();
        return view('index/index',['lianbiao'=>$lianbiao,'column'=>$column,'mokuai'=>$mokuai,'user'=>$user,'welcome'=>$welcome]);
    }
    public function welcome(){
        $welcome['fangwenliang'] = getSysTem('fangwen');
        if(!$welcome['fangwenliang']){
            $welcome['fangwenliang'] = 0;
        }
        $welcome['fujian'] = Db::name(getTable('enclosure'))
            ->count();
        $welcome['yonghu'] = Db::name(getTable('users'))
            ->count();
        return view('index/welcome',['welcome'=>$welcome]);
    }
    public function signin(Request $request){
        $modular = $request->param('modular',null);
        if($modular){
            session('modular',$modular);
        }
        if(!session('modular')){
            return redirect('index/modular');
        }
        return view();
    }
    public function signins(Request $request){
        $data =$request->param();
        $yanzhen = yanzhen($data,[
           'user|用户名'=>'require',
            'pwd|密码'=>'require',
            'code|验证码'=>'require|captcha'
        ]);
        if(!$yanzhen['code']){
            return json_to_respos($yanzhen['msg'],205);
        }
        $shuju = Db::name(session('modular').'_admin_user')
            ->where('user',$data['user'])
            ->find();
        if(!$shuju){
            return json_to_respos('用户不存在',205);
        }
        if(pwd_decode($data['pwd'],$shuju['pwd'])){
            session('adminuser',$shuju);
            return json_to_respos('登录成功');
        }else{
            return json_to_respos('账号或密码错误',206);
        }
    }

    /**加载选择模块页面
     * @param Request $request
     * @return \think\response\View
     */
    public function modular(Request $request){
        session('adminuser',null);
        session('modular',null);
        $dir = Modular::getDirList('../application',['admin']);
        $modular = config('modular.');
        return view('',['dir'=>$dir,'modular'=>$modular]);
    }

    /**加载添加模块页面
     * @return \think\response\View
     */
    public function addmodular(){
        return Builder::form()->setAction('index/set_modular')
            ->setProject('input','name','项目名称')
            ->setProject('input','dir','项目模块')
            ->setProject('input','author','作者')
            ->setProject('input','sql','数据库文件')
            ->setProject('input','img','图片地址','/static/admin/img/error.jpg')
            ->setButton('submit','提交')
            ->setButton('back','返回')
            ->fetch();
    }

    /**添加模块
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function set_modular(Request $request){
        $data = $request->param();
        $modular = config('modular.');
        foreach ($modular as $value){
            if($value['dir']==$data['dir']){
                echo '此模块已存在';
                exit;
            }
        }
        $modular = array_merge($modular,[$data]);
        if(Modular::changeConfig($modular)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function errors(Request $request){
        $code = $request->param('code');
        $action = $request->param('action',null);
        return view('public/error',['code'=>$code,'action'=>$action]);
    }

    /**安装模块
     * @param Request $request
     * @return \think\response\Json
     */
    public function install(Request $request){
        $data = $request->param('modular',null);
        if(!$data){
            json_to_respos('请传入要安装的模块',205);
        }
        $bool = Install::ins($data);
//        return $bool;
        if($bool){
            return json_to_respos('安装成功');
        }else{
            return json_to_respos('安装失败',205);
        }

    }

    /**本应用中间件
     * @param Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle(Request $request,\Closure $next){
        if($request->action()=='Index'){
            return $next($request);
        }
//        MyAuth::AuthYanZhen($request);
        if($request->isAjax()){
            if(!MyAuth::AuthYanZhen($request)){
                return json_to_respos('权限不足');
            }
        }else{
            if(!MyAuth::AuthYanZhen($request)){
                return response('权限不足');
            }
        }
        return $next($request);
    }
    public function out(){
        session('adminuser',null);
//        session('modular',null);
        return redirect('index/signin');
    }
    public function ces(){
        return MyAuth::change();
    }
}